#include "../inc/Ui/TwitterLoginForm.h"
#include "../inc/Ui/WebForm.h"

using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Social;
using namespace Osp::Social::Services;
int Form1::FormId = -1;
Form1::Form1(void) {
}

Form1::~Form1(void) {
}

bool Form1::Initialize() {
	// Construct an XML form
	Construct(L"Twitter_LoginForm");

	return true;
}

result Form1::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here

	// Get a button via resource ID
	__pButtonOk = static_cast<Button *> (GetControl(L"IDC_BUTTON_OK"));
	if (__pButtonOk != null)
	{
		__pButtonOk->SetActionId(ID_BUTTON_OK);
		__pButtonOk->AddActionEventListener(*this);
	}
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);

	__pAuthenticator = new SnsAuthenticator();
	__pAuthenticator->Construct(*this);
	pSnsGatewayManager=new MySnsGatewayManager();
	pSnsGatewayManager->Initialize();

	return r;
}
void Form1::ExcuteLogin() {
	if (pSnsGatewayManager-> CheckLoginStatus()) {
		__pButtonOk->SetEnabled(false);
	} else {
		__pButtonOk->SetEnabled(true);
	}
}
result Form1::OnTerminating(void) {
	result r = E_SUCCESS;

	if (__pAuthenticator) {
		delete __pAuthenticator;
		__pAuthenticator = null;
	}
	return r;
}

void Form1::OnActionPerformed(const Osp::Ui::Control& source, int actionId) {
	switch (actionId) {
	case ID_BUTTON_OK: {
		__pAuthenticator->Authenticate(L"twitter", L"TPd9grJbiqkcajkHvASE1w",L"tS7QRWB7PjhjLlM5BUy5G90H5ZZ6FhdCPAFJllMwO0");
		AppLog("OK Button is clicked! \n");
	}
	break;
	case ID_SOFTKEY_BACK:
	{
		ExcuteBack();
	}break;
	default:
	break;
}
}
void Form1::ExcuteBack() {

	/*pFrame->SetCurrentForm(*pFormParent);
	 (static_cast<WebForm*> (pFormParent))->CreateWebControl();
	 pFormParent-> RequestRedraw(true);
	 */
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);
	pFormManager->SetCurrentForm(WebForm::GetFormId());
	Form *pWebForm = null;
	result r = pFormManager->GetForm(WebForm::GetFormId(), pWebForm);
	if (IsFailed(r)) {
		return;
	}
	static_cast<WebForm*> (pWebForm)->CreateWebControl();

}

void Form1::OnSnsAuthenticated(
		Osp::Social::Services::SnsAuthResult& snsAuthResult, result r,
		const Osp::Base::String& errorCode, const Osp::Base::String& errorMsg) {

	if (IsFailed(r)) {
		AppLog("Fail : OnSnsAuthenticated[%s][%S][%S]", GetErrorMessage(r), errorCode.GetPointer(), errorMsg.GetPointer());
		return;
	}

	if (snsAuthResult.GetTokenKey().IsEmpty()) {
		AppLog("Fail, try again");
	} else {
		//Success to authentication
		AppLog("Thanh cong OnSnsAuthenticated");
		pSnsGatewayManager->AddTwitterAuthResult(snsAuthResult);
		pSnsGatewayManager->RequestMyUserNameFromProfile();
		pSnsGatewayManager->RequestActivities();
		//pFrame->SetCurrentForm(*pTwitterForm);
		//pTwitterForm->RequestRedraw(true);
		FormManager *pFormManager = null;
		Form *pTwitterForm = null;
		FormManager::GetInstance(pFormManager);
		pFormManager->SetCurrentForm(TwitterForm::GetFormId());
		result r =
				pFormManager->GetForm(TwitterForm::GetFormId(), pTwitterForm);
		AppLog("Lay dc form quan ly");
		if (IsFailed(r)) {
			return;
		}
		(static_cast<TwitterForm*> (pTwitterForm))->SetSnsGateWayTwitterForm(
				pSnsGatewayManager);
		AppLog("Th?c hi?n vi?c chuy?n goem");
	}
}

void Form1::SetLoginStatus(bool status) {
	if (status) {
		__pButtonOk->SetEnabled(true);
		pSnsGatewayManager->RequestMyUserNameFromProfile();
		pSnsGatewayManager->RequestActivities();
		AppLog("Form1:SetLoginStatus");
		//pFrame->SetCurrentForm(*pTwitterForm);
		//pTwitterForm->RequestRedraw(true);
		FormManager *pFormManager = null;
		Form *pTwitterForm = null;
		FormManager::GetInstance(pFormManager);
		pFormManager->SetCurrentForm(TwitterForm::GetFormId());
		result r =
				pFormManager->GetForm(TwitterForm::GetFormId(), pTwitterForm);
		if (IsFailed(r)) {
			return;
		}
		(static_cast<TwitterForm*> (pTwitterForm))->SetSnsGateWayTwitterForm(
				pSnsGatewayManager);
	} else {
		__pButtonOk->SetEnabled(true);
	}

	RequestRedraw(true);
}

MySnsGatewayManager *Form1::getMySnsGatewayManager() {
	return pSnsGatewayManager;
}
/*
 void Form1::SetParentForm(Form *pParent) {
 this->pFormParent = pParent;
 }

 void Form1::SetFrame(Frame *pFrame) {
 this->pFrame = pFrame;
 }*/
